const express = require('express')
      router = express.Router()

//注册
router.post('/register',(req,res) =>{
	req.body.gender = req.body.isMale
	req.body.createTime = new Date()
	var user = new User(req.body)
	user.save(function(err,data){
		if(err){
			send(res,'fail','系统错误，请稍后重试')
		}else{
			send(res,'success','恭喜您，注册成功')
		}
	})
	
})


router.get('/username/:username',(req,res) =>{
	var name = req.params.username
	User.find({username:name},function(err,data){
		if(err){
			send(res,'fail','很抱歉，系统错误')
		}else{
			if(data.length == 0){
				send(res,'success','恭喜可以注册')
			}else{
				send(res,'fail','很抱歉，用户名已存在')
				
			}
			
		}
	})
})


//头像上传
router.post('/header',uploads.single('header'),signIn,(req,res) =>{
	var name = req.cookies.username
	var headerName = req.file.filename
	if(req.file.mimetype.substr(0,5) == 'image'){
			User.update({username:name},{header:headerName},function(err,data){
	     if(err){
	     	send(res,'fail','系统错误，请稍后重试')
	     }else{
		     	User.find({username:name},function(err,data){
		     		res.cookie('header',data[0].header)
		     		send(res,'success','头像上传成功')
		     	})
	     }
		})
	}else{
		send(res,'fail','请上传一张图片')
	}
	
	
})


module.exports = router